#coding:utf-8
__author__ = 'hl'
import os
import zipfile
channelFileName = 'cztchannel'
def packaging(rawapkpath,apkpackagedpath,channels,):
    temp = rawapkpath
    #将'\'转换成'/'
    rawapkpath= rawapkpath.replace('\\','/')
    #删除渠道文件
    if not delFileSpecified(rawapkpath,'channel'):
        print u'删除表示渠道名称的文件失败'

    for channel in channels:
        #将apk包复制到保存路径下，并且改变他的名字
        cpcmd = 'copy '+ temp + ' '+apkpackagedpath+'\\'+channel[2]+'.apk'
        print cpcmd
        rs = os.system(cpcmd)
        if rs:
            print cpcmd+u'：复制失败'
            continue
        #向apk文件中加入文件
        zipf = zipfile.ZipFile(apkpackagedpath+'\\'+channel[2]+'.apk','a',zipfile.ZIP_DEFLATED)
        fname = 'META-INF/cztchannel_'+channel[2]
        zipf.write('czt.txt',fname)

        zipf.close()

def delFileSpecified(path,filename):
    cmd =  'aapt l '+path+' | findstr '+filename
    rsStr = os.popen(cmd)
    delpath = rsStr.read().strip()
    if not delpath:
        return 1
    cmd = 'aapt r '+path+' '+delpath
    rs = os.system(cmd)
    if not rs:
        return 1
    return 0